<?php
/**
 * Login_Data_Registred
 * registred user
 *
 * @author Adrian Stolarski
 */
class Login_Data_Registred implements Login_Data_Mainable, Login_Data_Preparable, 
        Login_Data_Messagable, Login_Data_Generatable, Login_Data_Sendable {
    
    /**
     *
     * @var type $password_generator
     * password generator handler
     */
    public $password_generator;
    /**
     *
     * @var type $database;
     * database handler
     */
    public $database;
    /**
     *
     * @var type $database query
     * database query handler
     */
    public $database_query;
    /**
     *
     * @var type $add_user_object
     * add user object
     */
    public $add_user_object;
    /**
     *
     * @var type $send_email
     * send email handler
     */
    public $email_sender;
    /**
     *
     * @var type $registered_message
     * registered message handler
     */
    public $registered_message;
    /**
     *
     * @var type $registered_action
     * registered action handler
     */
    public $redirect_action;
    /**
     * constructor()
     */
    public function __construct() {
        $this->database = new Core_Driver_MysqlImproved;
    }
    
    /**
     * main()
     * add new user
     * 
     * @param array $variables
     */
    public function main(array $variables = null) {
        
        $variables['password'] = $this->generate_password();
        $this->database->connect();
        foreach($variables as $key => $value){
            $variables[$key] = $this->database->escape($value);
        }
        $this->database->prepare($this->database_query);
        $this->database->query();
        $this->database->dataOperations();
        $this->add_user_object = new Login_Data_Setuserdata;
        $this->add_user_object->main($variables);
        $this->send_email($variables);
        $this->message('send');
    }
    
    /**
     * preapre_query()
     * preapre database query
     * 
     * @param array $variables
     */
    public function prepare_query(array $variables = null) {
        $this->database_query = "INSERT INTO "
                . "users_data("
                . "user_name, "
                . "user_email, "
                . "user_password)"
                . " values ('"
                .$variables['user_name'].
                "', '"
                .$variables['user_email'].
                "', '"
                .md5($variables['password']).
                "');";
    }
    
    /**
     * generate_password()
     * password generator
     * @return type $password
     */
    public function generate_password() {
        $this->password_generator = new Core_Controller_Randompassword;
        $password = $this->password_generator->main(null);
        return $password;
    }
    
    /**
     * message()
     * show user message and redirect to login
     * @param type $value
     */
    public function message($value) {
        $this->registered_message = new Login_Data_Registeredmessage;
        $this->registered_message->main(null);
        $this->redirect_action = new Login_Data_Loggedredirect;
        $this->redirect_action->main(null);
    }
    
    /**
     * send_email()
     * send email to user
     * 
     * @param array $variables
     */
    public function send_email(array $variables = null) {
        $this->email_sender = new Login_Data_Registredmailvalue;
        $this->email_sender->main($variables);
    }

}
